package GUI.TabPanel;

import java.awt.BorderLayout;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

import javax.swing.*;
import javax.swing.table.*;

public class FileView extends JScrollPane {

	private JPanel panel = new JPanel();
	private BoxLayout layout = new BoxLayout(panel, BoxLayout.Y_AXIS);
	private DefaultTableModel Model;
	private JTable table;
	private String Mac_ID;
	
	public FileView() {
		panel.setLayout(layout);
		this.setViewportView(panel);
		this.setVisible(true);
	}
	
	public void setMac_ID(String Mac_ID) {
		this.Mac_ID = Mac_ID;
	}
	
	public void reFresh(String filename) {
		Model = new DefaultTableModel();
		table = new JTable(Model);
		
		File src = new File(Mac_ID + "/Data/" + filename);
		
		if(src.exists()) {
			try {
				Scanner input = new Scanner(new FileInputStream(src));
				String[] header = input.nextLine().replaceAll("\"", "").split(",");
				for(String tmp : header) {
					Model.addColumn(tmp);
				}
				while(input.hasNext()) {
					String[] rowData = input.nextLine().replaceAll("\"", "").split(",");
					Model.addRow(rowData);
				}
				input.close();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		panel.removeAll();
		panel.add(table.getTableHeader());
		panel.add(table);
		
		panel.updateUI();
		this.updateUI();
	}
}
